﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DGIS.Map.Models;
using DGIS.Model;

namespace DGIS.Map.Templetes
{
    /// <summary>
    /// ProgressTemplete.xaml 的交互逻辑
    /// </summary>
    public partial class CompleteTaskTemplete : UserControl
    {
        public CompleteTaskTemplete()
        {
            InitializeComponent();
        }

        #region 附加属性



        public static string GetId(DependencyObject obj)
        {
            return (string)obj.GetValue(IdProperty);
        }

        public static void SetId(DependencyObject obj, Int64 value)
        {
            obj.SetValue(IdProperty, value);
        }

        // Using a DependencyProperty as the backing store for Id.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IdProperty =
            DependencyProperty.RegisterAttached("Id", typeof(string), typeof(CompleteTaskTemplete), new PropertyMetadata(""));



        public ImageSource IconSource
        {
            get { return (ImageSource)GetValue(IconSourceProperty); }
            set { SetValue(IconSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconSourceProperty =
            DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(CompleteTaskTemplete), new PropertyMetadata(null));



        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(CompleteTaskTemplete), new PropertyMetadata("未知"));



        public double FileSize
        {
            get { return (double)GetValue(FileSizeProperty); }
            set { SetValue(FileSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileSizeProperty =
            DependencyProperty.Register("FileSize", typeof(double), typeof(CompleteTaskTemplete), new PropertyMetadata(0.001));




        public double ProgressValue
        {
            get { return (double)GetValue(ProgressValueProperty); }
            set { SetValue(ProgressValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ProgressValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressValueProperty =
            DependencyProperty.Register("ProgressValue", typeof(double), typeof(CompleteTaskTemplete), new PropertyMetadata(0.0001));



        public string Md5
        {
            get { return (string)GetValue(Md5Property); }
            set { SetValue(Md5Property, value); }
        }

        // Using a DependencyProperty as the backing store for Md5.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Md5Property =
            DependencyProperty.Register("Md5", typeof(string), typeof(CompleteTaskTemplete), new PropertyMetadata(""));



        public static double GetSpeed(DependencyObject obj)
        {
            return (double)obj.GetValue(SpeedProperty);
        }

        public static void SetSpeed(DependencyObject obj, double value)
        {
            obj.SetValue(SpeedProperty, value);
        }

        // Using a DependencyProperty as the backing store for Speed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpeedProperty =
            DependencyProperty.RegisterAttached("Speed", typeof(double), typeof(CompleteTaskTemplete), new PropertyMetadata(0.01));


        public static IntPtr GetTaskIntPtr(DependencyObject obj)
        {
            return (IntPtr)obj.GetValue(TaskIntPtrProperty);
        }

        public static void SetTaskIntPtr(DependencyObject obj, IntPtr value)
        {
            obj.SetValue(TaskIntPtrProperty, value);
        }

        // Using a DependencyProperty as the backing store for TaskIntPtr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TaskIntPtrProperty =
            DependencyProperty.RegisterAttached("TaskIntPtr", typeof(IntPtr), typeof(CompleteTaskTemplete), new PropertyMetadata(IntPtr.Zero));



        public static Int64 GetStatus(DependencyObject obj)
        {
            return (Int64)obj.GetValue(StatusProperty);
        }

        public static void SetStatus(DependencyObject obj, Int64 value)
        {
            obj.SetValue(StatusProperty, value);
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.RegisterAttached("Status", typeof(Int64), typeof(CompleteTaskTemplete), new PropertyMetadata(Int64.MinValue));



        public static string GetSavePath(DependencyObject obj)
        {
            return (string)obj.GetValue(SavePathProperty);
        }

        public static void SetSavePath(DependencyObject obj, string value)
        {
            obj.SetValue(SavePathProperty, value);
        }

        // Using a DependencyProperty as the backing store for SavePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SavePathProperty =
            DependencyProperty.RegisterAttached("SavePath", typeof(string), typeof(CompleteTaskTemplete), new PropertyMetadata(""));



        #endregion

        #region 事件

        //删除任务事件
        private void Del_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MessageBox.Show("是否确认删除任务?", "提示", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                return;

            try
            {   
                string id = GetId(this);
                DGIS.Control.DownloadControl control = new Control.DownloadControl();
                if(control.DelCompleteFile(id.ToString()))
                {
                    string savePath = GetSavePath(this);
                    System.IO.File.Delete(savePath);
                    this.Height = 0;
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    MessageBox.Show("删除任务失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除任务失败");
            }
        }

        //打开文件夹事件
        private void OpenFolder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string savePath = GetSavePath(this);
            System.Diagnostics.Process.Start("Explorer.exe", "/select," + savePath);
        }

        #endregion        
    }
}
